/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.dataacesslayer;

import mnstudent.datatranferobject.DetailedRegistrationCoursesDTO;
import mnstudent.datatranferobject.RegistrationCoursesDTO;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author 11520_000
 */
public class RegistrationCoursesDAL {
  
    //<editor-fold defaultstate="collapsed" desc="Attributes">
    private RegistrationCoursesDTO m_RegistrationCoursesObj;
    private DataExecuteUseStoreProcedure m_DataExecute;
    private String  m_sqlCmd;
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Constuctor">
    public RegistrationCoursesDAL(){
        this.m_RegistrationCoursesObj = new RegistrationCoursesDTO();
        this.m_DataExecute = new DataExecuteUseStoreProcedure();    
        this.m_sqlCmd = "";
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Method">
    
     /**
     * Phuong thuc lay tat ca cac phieu dk trong bang PHIEUDANGKY
     *
     * @return ResultSet
     */
    public ResultSet getAllRegistrationCourses(){
        try
        {
            m_sqlCmd = "{Call GetAllRegistrationCoursesFromDatabase}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd,null);
            return _rsData;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    /**
     * Phuong thuc lay tat ca cac mon hoc(ma mon hoc, ten mon hoc, ten loai mon hoc, so tin chi) theo ma nganh
     * @param _idMajor
     * @return 
     */
    public ResultSet getAllSubjectDisplayByMajorID(String _idMajor)
    {
        try
        {
            m_sqlCmd = "{Call GetAllSubjectWithKindOfSubjectByMajorIDFromDatabase(?)}";
            String[] arrObj = {_idMajor};
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd,arrObj);
            return _rsData;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    public ResultSet getAllSubjectDisplayOpenBySemesterIDYearIDMajorID(String _idSemester, String _idYear, String _idMajor)
    {
        try
        {
            m_sqlCmd = "{Call GetAllSubjectDisplayOpenBySemesterIDYearIDMajorIDFromDatabase(?,?,?)}";
            String[] arrObj = {_idSemester, _idYear, _idMajor};
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd,arrObj);
            return _rsData;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    public ResultSet getMoneyOfCreditByNameKindOfSubject(String _nameKindOfSubject)
    {
         try
        {
            m_sqlCmd = "{Call GetMoneyOfCreditByNameKindOfSubjectFromDatabase(?)}";
            String[] arrObj = {_nameKindOfSubject};
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd,arrObj);
            return _rsData;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    /**
     * Ham lay khoa theo ma khoa
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getRegistrationCoursesById(String _id)
    {
        try {
            m_sqlCmd = "{Call GetRegistrationCoursesByIdFromDatabase(?)}";
            String[] arrId = {_id};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    /**
     * Ham lay khoa theo ma khoa
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getRegistrationCoursesByStudentId(String _idStudent)
    {
        try {
            m_sqlCmd = "{Call GetRegistrationCoursesByStudentIdFromDatabase(?)}";
            String[] arrId = {_idStudent};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    public ResultSet getRegistrationCoursesByStudentIdAndSemesterIDYearId(String _idStudent, String _idSemester, String _idYear)
    {
        try {
            m_sqlCmd = "{Call GetRegistrationCoursesByStudentIdAndSemesterIDYearIdFromDatabase(?,?,?)}";
            String[] arrId = {_idStudent,_idSemester, _idYear};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    public String getIdOfRegistrationCoursesByStudentIdAndSemesterIDYearId(String _idStudent, String _idSemester, String _idYear)
    {
        try {
            m_sqlCmd = "{Call GetRegistrationCoursesByStudentIdAndSemesterIDYearIdFromDatabase(?,?,?)}";
            String[] arrId = {_idStudent,_idSemester, _idYear};
            ResultSet rs = this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
            while(rs.next())
            {
                return rs.getNString(1);
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    public ResultSet getAllDetailedRegistrationCoursesByIdFromDatabase(String _idRegistration)
    {
        try {
            m_sqlCmd = "{Call GetAllDetailedRegistrationCoursesByIdFromDatabase(?)}";
            String[] arrId = {_idRegistration};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    public ResultSet getSubjectDisplayRegitrationBySubjectIdFromDatabase(String _idSubject)
    {
        try {
            m_sqlCmd = "{Call GetSubjectDisplayRegitrationBySubjectIdFromDatabase(?)}";
            String[] arrId = {_idSubject};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    /**
     * Phuong thuc lay len diem so lan sau cung cua mon hoc ma sinh vien da hoc
     * @param _idStudent, _idSubject
     * @return 
     */
    public ResultSet getAfterScoresSubjectOfStudentByStudentId(String _idStudent, String _idSubject)
    {
         try {
            m_sqlCmd = "{Call GetAfterScoresSubjectOfStudentByIdStudentFromDatabase(?,?)}";
            String[] arrId = {_idStudent, _idSubject};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    public ResultSet getIDegistrationCoursesFromDatabase() {
        try {
            m_sqlCmd = "{Call GetIDRegistrationCoursesFromDatabase}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd, null);
            return _rsData;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
    public boolean insertRegistrationCoursesToDatabase(RegistrationCoursesDTO _registrationCourses)
    {
        try {
           m_sqlCmd = "{Call InsertRegistrationCoursesToDatabase(?,?,?,?,?,?,?,?,?,?,?)}";
           Object[] arrStr = {_registrationCourses.getRegistrationCoursesID(), 
               _registrationCourses.getStudentID(),
               _registrationCourses.getDateOfRegistration(),
           _registrationCourses.getTotalTheoryCredit(),
           _registrationCourses.getTotalPracticeCredit(),
           _registrationCourses.getStaffID(),
           _registrationCourses.getSemesterID(),
           _registrationCourses.getAcademicYearID(),
           _registrationCourses.getTotalFee(),
           _registrationCourses.getTotalRemainingFee(),
           _registrationCourses
         .getAmountIsReduces()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    
    /**
     * Phuong thuc insert danh sach chi tiet phieu dk xuogn database
     * @param _arrDetailedRegistration
     * @return 
     */
    public boolean insertListSubjectRegistrationToDetailedRegistration(ArrayList<DetailedRegistrationCoursesDTO> _arrDetailedRegistration)
    {
        boolean rs = false;
        try {
            for (DetailedRegistrationCoursesDTO i : _arrDetailedRegistration) {
                if (insertDetailedRegistrationToDatabase(i)) {
                    rs = true;
                } else {
                    rs = false;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return rs;
    }
    /**
     * Phuong thuc insert chi tiet phieu dk xuogn database
     * @param obj
     * @return 
     */
    public boolean insertDetailedRegistrationToDatabase(DetailedRegistrationCoursesDTO obj)
    {
        try {
           m_sqlCmd = "{Call InsertDetailedRegistrationCoursesToDatabase(?,?)}";
           Object[] arrStr = { obj.getRegistrationFormID(), obj.getSubjectID()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    /**
     * Ham cap nhat sua doi thong phieu dk mon hoc
     * @param _registrationCourses
     * @return tue: thanh cong, false: that bai
     */
    public boolean updateRegistrationCoursesByIdToDatabase(RegistrationCoursesDTO _registrationCourses)
    {
       try {
           m_sqlCmd = "{Call UpdateRegistrationCoursesByIdToDatabase(?,?,?,?,?,?,?,?,?,?,?)}";
           Object[] arrStr = {_registrationCourses.getRegistrationCoursesID(), 
               _registrationCourses.getStudentID(),
               _registrationCourses.getDateOfRegistration(),
           _registrationCourses.getTotalTheoryCredit(),
           _registrationCourses.getTotalPracticeCredit(),
           _registrationCourses.getStaffID(),
           _registrationCourses.getSemesterID(),
           _registrationCourses.getAcademicYearID(),
           _registrationCourses.getTotalFee(),
           _registrationCourses.getTotalRemainingFee(),
           _registrationCourses.getAmountIsReduces()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
     /**
     * Ham cap nhat sua doi thong tin khoa
     * @param _registrationCourses
     * @return tue: thanh cong, false: that bai
     */
    public boolean deleteRegistrationCoursesByIdToDatabase(String _idRegistrationCourses)
    {
        try {
           m_sqlCmd = "{Call DeleteRegistrationCoursesByIdToDatabase(?)}";
           String[] arrStr = {_idRegistrationCourses};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    
    public boolean deleteRegistrationCoursesByStudentIdToDatabase(String _idStudent)
    {
        try {
           m_sqlCmd = "{Call DeleteRegistrationCoursesByStudentIdToDatabase(?)}";
           String[] arrStr = {_idStudent};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    
    public boolean deleteListDetailedRegistrationToDatabase(String _idRegistration)
    {
        try {
           m_sqlCmd = "{Call DeleteDetailedRegistrationCoursesByRegistrationIDToDatabase(?)}";
           Object[] arrStr = { _idRegistration};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    //</editor-fold>
}
